package com.ruoyi.business.rabbitmq.listener;

import com.ruoyi.business.domain.CollectedData;
import com.ruoyi.business.rabbitmq.AcquisitionProtocolConstants;
import com.ruoyi.business.rabbitmq.po.CollectDataInfo;
import com.ruoyi.business.rabbitmq.po.SignalInfo;
import com.ruoyi.business.service.ICollectedDataService;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class SignalListener {

    private List<CollectedData> machineOneCacheList = new ArrayList<>();

    private List<CollectedData> machineTwoCacheList = new ArrayList<>();

    private static final int INSERT_DB_THRESHOLD = 2000;

    private static Timer machineOneCacheTimer;

    private static Timer machineTwoCacheTimer;

    private static Map<String, Integer> countMap = new HashMap<>();

    @Autowired
    private ICollectedDataService collectedDataService;

    @Autowired
    private RedisCache redisCache;

//    @RabbitListener(queues = AcquisitionProtocolConstants.SIGNAL_QUEUE_NAME_ONE)
//    public void subscribeMachineOne(SignalInfo signalInfo) {
//        System.out.println("工控机一收到信号消息" + signalInfo.toString());
//    }

//    @RabbitListener(queues = AcquisitionProtocolConstants.SIGNAL_QUEUE_NAME, containerFactory = "firstFactory")
//    public void subscribeMachineOneRemoteSignalQueue(SignalInfo signalInfo) {
//        System.out.println("来自工控机一信号消息队列" + signalInfo.toString());
//    }
//
//    @RabbitListener(queues = AcquisitionProtocolConstants.SIGNAL_QUEUE_NAME, containerFactory = "secondFactory")
//    public void subscribeMachineTwoRemoteSignalQueue(SignalInfo signalInfo) {
//        System.out.println("来自工控机二信号消息队列" + signalInfo.toString());
//    }

    @RabbitListener(queues = AcquisitionProtocolConstants.DATA_UPLOAD_QUEUE_NAME_REMOTE, containerFactory = "firstFactory")
    public void subscribeMachineOneRemoteDataQueue(List<CollectedData> dataInfos) {
//        System.out.println("来自工控机一远程数据消息队列" + dataInfos.toString());
        machineOneCacheList.addAll(dataInfos);
        if (machineOneCacheList.size() >= INSERT_DB_THRESHOLD) {
            collectedDataService.insertCollectedDataBatch(machineOneCacheList);
            machineOneCacheList.clear();
        }
    }

    @RabbitListener(queues = AcquisitionProtocolConstants.DATA_UPLOAD_QUEUE_NAME_REMOTE, containerFactory = "secondFactory")
    public void subscribeMachineTwoRemoteDataQueue(List<CollectedData> dataInfos) {
        machineTwoCacheList.addAll(dataInfos);
//        System.out.println("来自工控机二远程数据消息队列" + machineTwoCacheList.size());
        if (machineTwoCacheList.size() >= INSERT_DB_THRESHOLD) {
//            System.out.println("插库");
            collectedDataService.insertCollectedDataBatch(machineTwoCacheList);
            machineTwoCacheList.clear();
        }
    }

    public void initCacheTimer() {

        countMap.put("machine_one_cache_count", machineOneCacheList.size());
        countMap.put("machine_two_cache_count", machineTwoCacheList.size());

        machineOneCacheTimer = new Timer();
        machineTwoCacheTimer = new Timer();


        machineOneCacheTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                checkAndInsert("machine_one_cache_count", machineOneCacheList, machineOneCacheTimer);
            }
        }, 3000, 3000);

        machineTwoCacheTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                checkAndInsert("machine_two_cache_count", machineTwoCacheList, machineTwoCacheTimer);
            }
        }, 3000, 3000);

        System.out.println("结束信号处理" + machineOneCacheList.size() + machineTwoCacheList.size());
    }

    public void checkAndInsert(String cacheCountKey, List<CollectedData> cache, Timer timer) {
        System.out.println("-------检测Cache-------" + cache.size());
        System.out.println("-------prevCount------" + countMap.get(cacheCountKey));
        // 检测cache数量还有没有变化
        if (cache.size() == countMap.get(cacheCountKey)) {
            System.out.println("-------触发触发插入库操作--------------------------------------");
            if (cache.size() != 0) {
                collectedDataService.insertCollectedDataBatch(cache);
            }
            cache.clear();
            timer.cancel();
            redisCache.setCacheObject("INSERT_DB_STATUS", false);
        } else {
            countMap.put(cacheCountKey, cache.size());
        }
    }

    public void initCache() {
        if (!machineOneCacheList.isEmpty()) {
            machineOneCacheList.clear();
        }
        if (!machineTwoCacheList.isEmpty()) {
            machineTwoCacheList.clear();
        }
        System.out.println("初始化缓存" + machineOneCacheList.size() + machineTwoCacheList.size());
    }
}
